import { Joystick } from "./Joystick";
import game from "./game";


const {ccclass, property} = cc._decorator;


var DIR = cc.Enum({
    UP : 0,
    RIGHT : 1,
    DOWN : 2,
    LEFT : 3
});

@ccclass
export default class Player extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = 'hello';

    speed:number=5;

    playerIndex:number=0;

     joyStick:Joystick = null;

     isMoveing:boolean;

    start () {
        this.joyStick = cc.find('UI/Joystick').getComponent(Joystick)
    }
    
    

     update (dt) {
        if(game.getInstance().playerIndex != this.playerIndex){
            return;
        }

        
        let x = this.joyStick.Joystick.x;
        let y = this.joyStick.Joystick.y
        let originalTile = game.getInstance().playerTile[this.playerIndex];
        if(y<0){
           
            let targetTile = cc.v2( originalTile.x,originalTile.y+1);
            cc.log('target')
            cc.log(targetTile.x,targetTile.y)
            
            if(targetTile && !game.getInstance().tryMoveToTargetTile(targetTile)){
                return;
            }
         
        }

        if(x<0){
           
            let targetTile = cc.v2( originalTile.x-1,originalTile.y);
            cc.log('target')
            cc.log(targetTile.x,targetTile.y)
            
            if(targetTile && !game.getInstance().tryMoveToTargetTile(targetTile)){
                return;
            }
         
        }

        if(x>0){
           
            let targetTile = cc.v2( originalTile.x+1,originalTile.y);
            cc.log('target')
            cc.log(targetTile.x,targetTile.y)
            
            if(targetTile && !game.getInstance().tryMoveToTargetTile(targetTile)){
                return;
            }
         
        }

         if(y>0){
            let targetTile = cc.v2( originalTile.x, originalTile.y-1);
            cc.log('target')
            cc.log(targetTile.x,targetTile.y)
            if( targetTile !game.getInstance().tryMoveToTargetTile(targetTile)){
                return;
            }
        }

        if(x!=0){
            this.node.x+=x*dt;
            this.node.y+=y*dt;
        }

        // if(x<0){
        //     let playerTargetTile = cc.v2(this.node.x, this.node.y - 1);
        // }
        
        cc.log(this.joyStick.Joystick.x,this.joyStick.Joystick.y)
       
     }
}
